package com.typhoon.spring_shiro.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

/**
 * Properties文件载入工具类. 可载入多个properties文件, 相同的属性在最后载入的文件中的值将会覆盖之前的值.
 */
public class PropertiesUtils {

	private static ResourceLoader RESOURCE_LOADER = new DefaultResourceLoader();


	private PropertiesUtils() {
		
	}
	
	/**
     * 载入多个文件. 文件路径使用Spring Resource格式.
     */
    public static Properties loadProperty(String resourcePath) {
        Properties properties = new Properties();
        InputStream is = null;
        try {
            Resource resource = RESOURCE_LOADER.getResource(resourcePath);
            is = resource.getInputStream();
            properties.load(is);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return properties;
    }
	
	/**
	 * 载入多个文件. 文件路径使用Spring Resource格式.
	 */
	public static Properties loadProperties(String... resourcesPaths) {
		Properties properties = new Properties();
		for (String location : resourcesPaths) {
			InputStream is = null;
			try {
				Resource resource = RESOURCE_LOADER.getResource(location);
				is = resource.getInputStream();
				properties.load(is);
			} catch (IOException ex) {
				ex.printStackTrace();
			} finally {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return properties;
	}


    /**
     * 过滤原始props中的key，把包含指定的前缀的key和其映射的value组织成一个新的properties并返回
     * 
     * @param properties 原始props
     * @param prefix key前缀
     * @return
     * @date 2016年1月5日
     * @since V2.2.0
     * @author Ternence
     */
    public static Properties filter(Properties properties, String prefix) {
        Properties newProperties = new Properties();
        if (properties == null || properties.isEmpty()) {
            return newProperties;
        }

        for (Object key : properties.keySet()) {
            if (key != null && key.toString().startsWith(prefix)) {
                newProperties.put(key, properties.get(key));
            }
        }

        return newProperties;

    }

    /**
     * 读取配置文件并返回全部KV对
     * 
     * @param fileName 配置文件名称 ，可以是classpath下面的文件
     * @return
     * @date 2015年12月30日
     * @since V2.2.0
     * @author Ternence
     */
    public static Properties loadAllProperties(String fileName) {
        Properties properties = new Properties();
        InputStream is = null;
        try {
            Resource resource = RESOURCE_LOADER.getResource(fileName);

            if (resource == null || !resource.exists()) {
                return properties;
            }

            is = resource.getInputStream();
            properties.load(is);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return properties;
    }
}
